﻿using System;
using System.Linq;
 
using AmazeUI.db;
using AmazeUI.Imodel;

namespace AmazeUI.bll
{
    public class QixiBll:BaseBll
    {
        public   int Add(IQixi model)
        {
            var qixi = new Qixi(model);
            this.db.Qixi.Add(qixi);
            this.db.SaveChanges();

            return qixi.Id;
        }

        public Qixi  Getmodel(int id)
        {
            return this.db.Qixi.Find(id);
             
        }

        public int Getyizhuan()
        {
            return this.db.Qixi.Count();
        }

        /// <summary>
        /// 获取阅读量
        /// </summary>
        /// <returns></returns>
        public string Getyuedu()
        {
            var appKey = this.db.XtAppKey.FirstOrDefault(a => a.Key == "qixiyuedu");
            if (appKey==null)
            {
                var ak = new XtAppKey()
                {
                    Key = "qixiyuedu",
                    Value = "1",
                    UpValue = "1",
                    UpTime = DateTime.Now

                };
                this.db.XtAppKey.Add(ak);
                this.db.SaveChanges();
                return ak.Value;
            }

            var av = appKey.Value;
            appKey.UpValue = av;
            appKey.Value = (Convert.ToInt32(appKey.Value) + 1).ToString();
            appKey.UpTime = DateTime.Now;
            this.db.SaveChanges();
            return appKey.Value;
        }
        /// <summary>
        /// 获取阅读量
        /// </summary>
        /// <returns></returns>
        public string Getyuedu(int v)
        {
            var appKey = this.db.XtAppKey.FirstOrDefault(a => a.Key == "qixiyuedu");
            if (appKey == null)
            {
                var ak = new XtAppKey()
                {
                    Key = "qixiyuedu",
                    Value = "1",
                    UpValue = "1",
                    UpTime = DateTime.Now

                };
                this.db.XtAppKey.Add(ak);
                this.db.SaveChanges();
                return ak.Value;
            }

            return appKey.Value;
        }
    }
}